package com.sysd.server.handler.base;

import com.sysd.bean.ErrorMessage;
import com.sysd.server.container.ChannelContainer;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * @description:
 * @author: bsh
 * @date: 2025/6/14 13:21
 */
@Slf4j
public abstract class AbstractBaseHandler<T> extends SimpleChannelInboundHandler<T> {

    @Resource
    protected ChannelContainer channelContainer;

    /**
     * 自定义的消息处理
     * @param ctx 上下文
     * @param msg 消息
     */
    public abstract void channelRead1(ChannelHandlerContext ctx, T msg);
    /**
     * @param ctx   上下文
     * @param cause 异常
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("程序异常:{}", cause.getMessage());
        ctx.writeAndFlush(ErrorMessage.defaultError("程序异常,连接关闭!")).addListener(ChannelFutureListener.CLOSE);
    }
}
